﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using _2_Services;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 管理员管理
    /// </summary>
    public class AdminService : IAdminService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public AdminService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 管理员注册
        /// </summary>
        /// <param name="adminRegisterDto"></param>
        /// <returns></returns>
        public bool AdminRegister(AdminRegisterDto adminRegisterDto)
        {
            bool result = false;
            if (_db.Queryable<AdminList>()
                .Where(x => x.AdminName == adminRegisterDto.AdminName)
                .Count() == 0)
            {
                AdminList adminList = _mapper.Map<AdminList>(adminRegisterDto);

                if (_db.Insertable(adminList).ExecuteCommand() == 1)
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 管理员登陆
        /// </summary>
        /// <param name="adminName">用户名</param>
        /// <param name="adminPassword">密码</param>
        /// <returns></returns>
        public AdminListDto AdminLogin(string adminName, string adminPassword)
        {
            return _db.Queryable<AdminList>()
                .Where(x => x.AdminName == adminName)
                .Where(x => x.AdminPassword == adminPassword)
                .Select<AdminListDto>()
                .First();
        }

        /// <summary>
        /// 上传图片,通过Form表单提交
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public string UploadPictures(IFormFile file)
        {
            string message;
            try
            {
                if (file != null)
                {
                    #region  图片文件的条件判断
                    //文件后缀
                    var fileExtension = Path.GetExtension(file.FileName);

                    //判断后缀是否是图片
                    const string fileFilt = ".gif|.jpg|.jpeg|.png";
                    if (fileExtension == null)
                    {
                        return message = "上传的文件没有后缀";
                    }
                    if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
                    {
                        return message = "请上传jpg、png、gif格式的图片";
                    }

                    //判断文件大小    
                    long length = file.Length;
                    if (length > 1024 * 1024 * 10) //10M
                    {
                        return message = "上传的文件不能大于10M";
                    }
                    #endregion

                    //图片重命名
                    string strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff"); //取得时间字符串
                    string strRan = Convert.ToString(new Random().Next(100, 999)); //生成三位随机数
                    string saveName = strDateTime + strRan + fileExtension;

                    //如果目标文件夹不存在，则新建目标文件夹
                    string folder = "Images/Cjw";
                    string imagePath = "./" + folder;
                    if (!Directory.Exists(imagePath))
                    {
                        Directory.CreateDirectory(imagePath);
                    }

                    //插入图片数据                 
                    using (FileStream fs = System.IO.File.Create(imagePath + "/" + saveName))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }

                    return message = "https://118.31.170.229:5001/" + folder + "/" + saveName;
                }
                else
                {
                    return message = "图片为空";
                }
            }
            catch (Exception ex)
            {
                return message = "上传失败";
            }
        }

        /// <summary>
        /// 获取员工信息列表
        /// </summary>
        /// <param name="adminName">员工姓名</param>
        /// <param name="sex">1、0</param>
        /// <param name="position">、前台管理、护士、医生</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public GetAdminListDto GetAdminList(string adminName, string sex, string position, int pageIndex, int pageSize)
        {
            GetAdminListDto getAdminListDto = new GetAdminListDto();
            int totalCount = 0;
            getAdminListDto.adminLis = _db.Queryable<AdminList>()
                .WhereIF(adminName != null, x => x.AdminName.Contains(adminName))
                .WhereIF(sex != null, x => x.Sex.Equals(sex))
                .WhereIF(position == null, x => x.Position != "管理员")
                .WhereIF(position != null, x => x.Position == position)
                .ToPageList(pageIndex, pageSize, ref totalCount);
            getAdminListDto.Total = totalCount;
            getAdminListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return getAdminListDto;
        }

        /// <summary>
        /// 修改员工个人信息
        /// </summary>
        /// <param name="adminList"></param>
        /// <returns></returns>
        public bool UpdateAdmin(AdminList adminList)
        {
            return _db.Updateable(adminList).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public List<AdminList> GetAdminListByPosition(string position)
        {
            return _db.Queryable<AdminList>()
                .WhereIF(position != null, x => x.Position == position)
                .ToList();
        }

        /// <summary>
        /// 获取个人信息
        /// </summary>
        /// <param name="adminName">用户名</param>
        /// <returns></returns>
        public AdminList GetMyInformationByAdminName(string adminName)
        {
            return _db.Queryable<AdminList>()
                .Where(x => x.AdminName == adminName)
                .First();
        }
    }
}
